using System.Data.Entity;
using CPlatePrintSys.Domain.Models;

namespace CPlatePrintSys.DataAccess.Context
{
    public class CPlateDbContext : DbContext
    {
        public CPlateDbContext() : base("name=CPlateConnectionString")
        {
        }

        public DbSet<User> Users { get; set; }
        public DbSet<MotherShip> MotherShips { get; set; }
        public DbSet<StreamShip> StreamShips { get; set; }
        public DbSet<PlateAnnotationInfo> PlateAnnotationInfos { get; set; }
        public DbSet<GlobalConfig> GlobalConfigs { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // User表配置
            modelBuilder.Entity<User>()
                .ToTable("User")
                .HasKey(u => u.Id);

            modelBuilder.Entity<User>()
                .Property(u => u.Username)
                .IsRequired()
                .HasMaxLength(20);

            modelBuilder.Entity<User>()
                .Property(u => u.Password)
                .IsRequired()
                .HasMaxLength(20);

            modelBuilder.Entity<User>()
                .Property(u => u.JobNumber)
                .IsRequired()
                .HasMaxLength(20);

            modelBuilder.Entity<User>()
                .Property(u => u.FullName)
                .HasMaxLength(100);

            modelBuilder.Entity<User>()
                .Property(u => u.Role)
                .HasColumnType("int");

            // 添加唯一索引
            modelBuilder.Entity<User>()
                .HasIndex(u => u.Username)
                .IsUnique();

            modelBuilder.Entity<User>()
                .HasIndex(u => u.JobNumber)
                .IsUnique();

            modelBuilder.Entity<MotherShip>()
                .HasMany(m => m.StreamShips)
                .WithRequired(s => s.MotherShip)
                .HasForeignKey(s => s.MotherShipId);

            modelBuilder.Entity<MotherShip>()
                .Property(m => m.ShipNo)
                .IsRequired()
                .HasMaxLength(20);

            modelBuilder.Entity<StreamShip>()
                .Property(s => s.ShipNo)
                .IsRequired()
                .HasMaxLength(20);

            modelBuilder.Entity<PlateAnnotationInfo>()
                .ToTable("PlateAnnotationInfo")
                .HasKey(p => p.Id);

            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.MotherShipNo).HasMaxLength(20);
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.StreamShipNo).HasMaxLength(20);
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.LotNo).HasMaxLength(20);
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.PlateNo).HasMaxLength(20);
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.Sequence).HasMaxLength(20);
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.CNO).HasMaxLength(20);
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.DesignSpecCls).HasMaxLength(50);
            
            // 数值类型字段配置 - 使用DOUBLE类型以确保MySQL兼容性
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.DesignSpecThickness).HasColumnType("DOUBLE");
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.DesignSpecLength).HasColumnType("DOUBLE");
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.DesignSpecWidth).HasColumnType("DOUBLE");
            
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.SheetConsistentNo).HasMaxLength(50);
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.SheetSpecCls).HasMaxLength(50);
            
            // 钢板规格数值类型字段配置 - 使用DOUBLE类型以确保MySQL兼容性
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.SheetSpecThickness).HasColumnType("DOUBLE");
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.SheetSpecLength).HasColumnType("DOUBLE");
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.SheetSpecWidth).HasColumnType("DOUBLE");
            
            // 布局相关字段配置
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.LayoutMean).HasColumnType("INT");
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.MergedInfo).HasMaxLength(100);
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.LayoutInfo)
                .HasColumnType("TEXT");
            
            // 日期字段配置
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.PlanUsedDate).HasColumnType("DATETIME");
            
            // 布尔字段配置 - 使用BIT类型以确保MySQL EF兼容性
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.IsAnnotationGenerated).HasColumnType("BIT");
            
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.DesignDrawingPath).HasMaxLength(200);
            
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.AnnotationDrawingPath).HasMaxLength(200);
            
            // 数据来源类型配置
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Property(p => p.SourceType).HasColumnType("INT");

            // 排除IsSelected属性，因为它不应该存储在数据库中
            modelBuilder.Entity<PlateAnnotationInfo>()
                .Ignore(p => p.IsSelected);

            // 添加索引配置
            modelBuilder.Entity<PlateAnnotationInfo>()
                .HasIndex(p => new { p.StreamShipNo, p.LotNo, p.Sequence })
                .HasName("idx_stream_ship_lot_sequence");
            
            modelBuilder.Entity<PlateAnnotationInfo>()
                .HasIndex(p => p.MotherShipNo)
                .HasName("idx_mother_ship_no");
                
            modelBuilder.Entity<PlateAnnotationInfo>()
                .HasIndex(p => p.PlateNo)
                .HasName("idx_plate_no");
                
            modelBuilder.Entity<PlateAnnotationInfo>()
                .HasIndex(p => p.CNO)
                .HasName("idx_cno");
                
            modelBuilder.Entity<PlateAnnotationInfo>()
                .HasIndex(p => p.PlanUsedDate)
                .HasName("idx_plan_used_date");
                
            modelBuilder.Entity<PlateAnnotationInfo>()
                .HasIndex(p => p.SourceType)
                .HasName("idx_source_type");
                
            modelBuilder.Entity<PlateAnnotationInfo>()
                .HasIndex(p => p.LayoutMean)
                .HasName("idx_layout_mean");
                
            modelBuilder.Entity<PlateAnnotationInfo>()
                .HasIndex(p => p.IsAnnotationGenerated)
                .HasName("idx_is_annotation_generated");

            // GlobalConfig表配置
            modelBuilder.Entity<GlobalConfig>()
                .ToTable("GlobalConfig")
                .HasKey(g => g.Id);

            modelBuilder.Entity<GlobalConfig>()
                .Property(g => g.ConfigKey)
                .IsRequired()
                .HasMaxLength(64);

            modelBuilder.Entity<GlobalConfig>()
                .Property(g => g.ConfigValue)
                .IsRequired();

            modelBuilder.Entity<GlobalConfig>()
                .Property(g => g.Description)
                .HasMaxLength(255);

            modelBuilder.Entity<GlobalConfig>()
                .Property(g => g.UpdatedBy)
                .HasMaxLength(64);

            // 添加唯一索引
            modelBuilder.Entity<GlobalConfig>()
                .HasIndex(g => g.ConfigKey)
                .IsUnique();

            base.OnModelCreating(modelBuilder);
        }
    }
} 